﻿param($installPath, $toolsPath, $package, $project)

function TryCommentSection($xml, $xpath) {

	$sectionsCount = $xml.SelectNodes($xpath).Count

	$shouldCommentSection = $sectionsCount -gt 1

	if ($shouldCommentSection) {

		$sectionNode = $xml.SelectSingleNode($xpath)

		$documentFragment = $xml.CreateDocumentFragment()
		$documentFragment.InnerXml = "<!--" + $sectionNode.OuterXml + "-->"

		$sectionNode.ParentNode.ReplaceChild($documentFragment, $sectionNode)
	}
}

$xml = New-Object xml

# find the Web.config file
$config = $project.ProjectItems | where {$_.Name -eq "Web.config"}

if (!$config) {
	return
}

# find its path on the file system
$localPath = $config.Properties | where {$_.Name -eq "LocalPath"}

# load Web.config as XML
$xml.Load($localPath.Value)

# try comment customErrors
TryCommentSection $xml "configuration/system.web/customErrors"

#try comment httpErrors
TryCommentSection $xml "configuration/system.webServer/httpErrors"

$xml.Save($localPath.Value)
